package com.yyl.fmall.utils;

import com.github.wxpay.sdk.WXPay;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: fmall
 * @BelongsPackage: com.yyl.fmall.utils
 * @Author: YuanSir
 * @CreateTime: 2022-11-11  09:13
 * @Description:
 * @Version: 1.0
 */

public class WxpayUtils {
    
    public static String getPayUrl(Map<String, Object> order) throws Exception {
        Integer actualAmount = (Integer) order.get("actualAmount");
        //设置当前订单信息
        HashMap<String,String> data = new HashMap<>();
        data.put("body", (String) order.get("untitled"));  //商品描述
        data.put("out_trade_no", (String) order.get("orderId"));               //使用当前用户订单的编号作为当前支付交易的交易号
        data.put("fee_type","CNY");                     //支付币种
        //data.put("total_fee",order.getActualAmount()*100+"");          //支付金额
        data.put("total_fee", String.valueOf(actualAmount * 100));
        data.put("trade_type","NATIVE");                //交易类型
        data.put("notify_url","http://47.118.45.73:8080/pay/callback");           //设置支付完成时的回调方法接口
        
        //发送请求，获取响应
        //微信支付：申请支付连接
        WXPay wxPay = new WXPay(new MyPayConfig());
        //微信支付平台的统一下单接口
        Map<String, String> resp = wxPay.unifiedOrder(data);
        return resp.get("code_url");
    }
    
    
    public static String queryOrder(String orderId) throws Exception {
        HashMap<String, String> params = new HashMap<>();
        params.put("out_trade_no", orderId);
        WXPay wxPay = new WXPay(new MyPayConfig());
        Map<String, String> resp = wxPay.orderQuery(params);
        return resp.get("trade_state");
    }
    
    public static void main(String[] args) throws Exception {
        
        String orderId = "20221111092930123456"; //订单编号
 
//        String payUrl = getPayUrl(orderId);
//
//        System.out.println("payUrl = " + payUrl);
    
        String s = queryOrder(orderId);
        System.out.println("s = " + s);
    
    }

}
